﻿using AccountUser = NC.Core.Models.AccountUser;
using DataAccountUser = NC.DAL.AccountUser;
using AutoMapper;
using NC.DAL;

namespace NC.Core
{
    /// <summary>
    /// This calls the account repository after processing the account user information it receives from the web layer
    /// </summary>
    public class AccountUserService: IAccountUserService
    {
        private IAccountUserRepository _accountUserRepository;
        public AccountUserService()
        {
            _accountUserRepository = new AccountUserRepository();
            Mapper.CreateMap<DataAccountUser, AccountUser>();
            Mapper.CreateMap<AccountUser, DataAccountUser>();
        }

        public AccountUser RegisterAccountUser(AccountUser accountUser)
        {
            var dataAccountUser = Mapper.Map<DataAccountUser>(accountUser);
            dataAccountUser = _accountUserRepository.RegisterAccountUser(dataAccountUser);
            return Mapper.Map<AccountUser>(dataAccountUser);
        }


        public bool ValidateUser(string email, string password)
        {
            var userdata=_accountUserRepository.ValidateUser(email, password);
            if (userdata != null)
            {
                return true;
            }
            return false;
            
        }
        public bool ActivateUser(string activationCode)
        {
            var dataAccountUser = _accountUserRepository.GetAccountUserByActivationCode(activationCode);
            if (dataAccountUser != null && !dataAccountUser.Active)
            {
                _accountUserRepository.ActivateUser(dataAccountUser.Id);
                return true;
            }
            return false;
        }

       public bool IsDuplicateEmail(string email)
       {
           return (_accountUserRepository.IsDuplicateEmail(email));
       }
    }
}
